Skip to content

Conversation

@fionaRowan
Copy link

@fionaRowan fionaRowan commented Oct 14, 2025

related to issue #6801 , though takes a different approach than that requested in the issue description. this PR tentatively implements solution 1 outlined in this issue comment, though could pivot to another solution if preferred!

Summary

currently, when configuring the slug, the template tags {{year}}, {{month}}, etc are always pulled from the hardcoded "now".

in this PR, if the entry contains an inferred 'date' field, we use that value when generating the slug.

thus, the following slug config would pull the template tags {{year}}, {{month}}, etc from the entry's front matter inferred 'date' field if it exists (or otherwise, "now"):

slug: "{{year}}-{{month}}-{{day}}-{{slug}}" 

notes for evaluating slugFormatter's expected behavior w.r.t. date template tags:

| slug | Template for filenames. {{year}}, {{month}}, and {{day}} pulls from the post's date field or save date. {{slug}} is a URL-safe version of the post's title. Default is simply {{slug}}. |

  • however, this PR makes the slugFormatter's behavior more nuanced than this piece of documented behavior implies

{{year}}: 4-digit year of the file creation date

Test plan

  • wrote a unit test which I verified passes
  • (still working on this) built the library + pointed my own static generator site to that library locally to verify that the slug template variables {{year}}, {{month}}, etc are pulled from the entry's front matter "date" field if it exists

Checklist

Please add a x inside each checkbox:

A picture of a cute animal (not mandatory but encouraged)
signal-2025-06-15-204636_002

@fionaRowan fionaRowan requested a review from a team as a code owner October 14, 2025 21:23
@martinjagodic
Copy link
Member

@fionaRowan this is a very well-written PR!

I like the idea of your second point: allow the user to configure which date to use. For instance, Hugo also uses publishDate, and it would be helpful to have that choice.

But, how should the user pass that field?

option 1:

slug: "{{fields.date.year}}-{{fields.date.month}}-{{fields.date.day}}-{{slug}}"

The original proposal from #6801. It feels too verbose, and .year sounds like a field, not a modifier.

option 2:

slug: "{{fields.date | date("YYYY-MM-DD)}}-{{slug}}"

This matches your option 3, I like it better. If I understand correctly, this is possible if we merge #6690?

In any case, I would first merge what you did here, because it's a well-rounded PR. Just let me know once you complete the testing, and please share your thoughts on how to continue.

@fionaRowan fionaRowan force-pushed the 6801-slug-uses-date-from-entry-if-exists branch from 01ba64f to e0128c5 Compare October 16, 2025 15:02
@fionaRowan
Copy link
Author

thanks for the thorough review, @martinjagodic ! to respond to some of your feedback and questions below~

I like the idea of your second point: allow the user to configure which date to use. For instance, Hugo also uses publishDate, and it would be helpful to have that choice.

But, how should the user pass that field?

I believe inferring the date field takes into account "synonyms", so this change should result in picking the first of those date “synonyms” - I added a couple tests in my last commit to be sure.

that said, if we'd prefer not to infer which date field to use in this case, I could see value in introducing a new config variable, similar to preview_path_date_field - curious what you think or if you have a preference either way?

The original proposal from #6801. It feels too verbose, and .year sounds like a field, not a modifier.

I agree with your take here! thanks for the insight!

This matches your option 3, I like it better. If I understand correctly, this is possible if we merge #6690?

I also like this option overall better, and overall I think there'd be value in merging #6690 as is to start supporting filtering in slug configs. some small caveats worth mentioning based on my own minimal unit testing:

lastly, it appears date handling varies depending on replacement method in stringTemplate#compileStringTemplate (though this behavior may already exist for summary config filtering) - take the following examples, which to my mind should be functionally equivalent in seeing a date field in the entry pulled and filtered as expected (based on minimal unit testing)~

  1. slug: "{{date | date("YYYY-MM-DD)}}-{{slug}}" feat(slugs): allow filters in template strings #6690 is sufficient
  2. slug: "{{fields.date | date("YYYY-MM-DD)}}-{{slug}}" feat(slugs): allow filters in template strings #6690 is not sufficient

the difference between the above two examples appears to be a difference in string serialization~

there could be value in tracking the diverging behavior in date handling in a small follow-up issue - would be happy to file one if you agree these should be functionally equivalent examples!

@fionaRowan
Copy link
Author

(I still plan to manually test my change later today, but happy to go for another implementation here or close this in favor of #6690 ! will defer to your insight here. thanks again for the review!)

@fionaRowan
Copy link
Author

@martinjagodic finally wrapped up my manual testing~ below are the steps I took:

  1. run npm run build on this branch of the repo
  2. copy the following files into my website's source directory so I could reference a static, local copy of decap:
fionarowan@MacBookPro decam-cms % cp ./packages/decap-cms/dist/decap-cms.js /path/to/website/src/js/decap-cms.js 
fionarowan@MacBookPro decam-cms % cp ./packages/decap-cms/dist/decap-cms.js.map /path/to/website/src/js/decap-cms.js.map
  1. replace reference to official decap package with reference to static local copy in our admin page template:
<script src="/js/decap-cms.js"></script>
<script src="/js/decap-cms.js.map"></script>
  1. ensure slug config uses date template tags~ slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
  2. run website and decap server against local git gateway backend as per these instructions
  3. verify that the file slug's tags {{year}}, {{month}}, {{day}} are populated by the date entered in the entry date widget in the UI
Screenshot 2025-10-19 at 11 43 48 AM

resulting file name

on this branch: _collections/events/2025-11-26-test-future-event-4.md

on main: _collections/events/2025-10-19-test-future-event-4.md

@martinjagodic martinjagodic enabled auto-merge (squash) October 28, 2025 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants